using System;
using UnityEngine;
using UnityEngine.UI;

namespace UIFramework.Examples
{
    [RequireComponent(typeof(Button))]
    public class NavigationPanelButton : MonoBehaviour
    {
        [SerializeField] 
        private Text buttonLabel = null;

        [SerializeField] 
        private Image icon = null;

        public event Action<NavigationPanelButton> ButtonClicked;

        private NavigationPanelEntry navigationData = null;
        private Button _button = null;
        private Button button 
        {
            get {
                if (_button == null) 
                {
                    _button = GetComponent<Button>();
                }

                return _button;
            }
        }
 
        public string Target 
        {
            get { return navigationData.TargetScreen; }
        }

        /// <summary>
        /// 设置按钮初始显示相关
        /// </summary>
        /// <param name="target"></param>
        public void SetData(NavigationPanelEntry target) 
        {
            navigationData = target;
            buttonLabel.text = target.ButtonText;
            icon.sprite = target.Sprite;
        }

        /// <summary>
        /// 设置按钮可交互状态
        /// </summary>
        /// <param name="selectedButton"></param>
        public void SetCurrentNavigationTarget(NavigationPanelButton selectedButton) 
        {
            button.interactable = selectedButton != this;
        }

        /// <summary>
        /// 设置按钮可交互状态
        /// </summary>
        /// <param name="screenId"></param>
        public void SetCurrentNavigationTarget(string screenId) 
        {
            if (navigationData != null) 
            {
                button.interactable = navigationData.TargetScreen == screenId;
            }
        }
        
        /// <summary>
        /// 外部调用点击
        /// </summary>
        public void UI_Click() 
        {
            if (ButtonClicked != null) 
            {
                ButtonClicked(this);
            }
        }
        
        
    }
}